import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:qianyi/pages/home/community/page_view/recommend/index.dart';
import 'package:qianyi/wcao/ui/theme.dart';

import '../../../ui/home/myDrawer.dart';
import '../publish/index.dart';
import 'page_view/found/index.dart';
import 'page_view/like/index.dart';

/// HACK: 社区

class PageViewCommunity extends StatefulWidget {
  const PageViewCommunity({Key? key}) : super(key: key);

  @override
  State<PageViewCommunity> createState() => _PageViewCommunityState();
}

class _PageViewCommunityState extends State<PageViewCommunity>
    with TickerProviderStateMixin,AutomaticKeepAliveClientMixin {
  int curPage = 1;

  final List<Tab> _tabs = const [

    Tab(text: "关注",height: 30,),
    Tab(text: "推荐",height: 30),
    // Tab(text: "发现",height: 30),
  ];
  final List<String> tabs = ["关注", "推荐"];

  late PageController _pageController;
  late TabController _tabController;

  @override
  void initState() {
    super.initState();

    _tabController = TabController(length: _tabs.length, vsync: this);
    _pageController = PageController(initialPage: curPage);
  }

  @override
  Widget build(BuildContext context) {
    SystemChrome.setSystemUIOverlayStyle(
      SystemUiOverlayStyle.light.copyWith(
        systemNavigationBarIconBrightness: Brightness.dark,
        systemNavigationBarColor: Colors.white,
        statusBarIconBrightness: Brightness.dark,
        statusBarColor: Colors.white, // Note RED here
      ),
    );
    return SafeArea(

        // bottom: false,top: false,
        child: DefaultTabController(

        initialIndex: curPage,
        length: _tabs.length,
        child: Scaffold(
          drawer: MyDrawer(),
          body: NestedScrollView(
            physics: NeverScrollableScrollPhysics(),
            headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
              return <Widget>[
                // 可替换成 SliverPinnedHeader
                SliverAppBar(
                  primary: false,
                  toolbarHeight: 35,
                  pinned: true,
                  leading: Image(

                    image: AssetImage("assets/icon/huomiao.png"),
                    fit: BoxFit.fitHeight, // 根据需要选择 BoxFit
                  ),
                  // backgroundColor: Colors.transparent,
                  flexibleSpace: TabBar(
                    padding: EdgeInsets.symmetric(horizontal: 60),
                    indicatorColor: WcaoTheme.primary,
                    indicatorSize: TabBarIndicatorSize.tab,
                    unselectedLabelColor: WcaoTheme.indexBootBarColor,
                    unselectedLabelStyle: const TextStyle(fontWeight: FontWeight.normal),
                    labelColor: WcaoTheme.indexBootBarActColor,
                    labelStyle: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                    ),

                    indicatorWeight: 2,
                    indicatorPadding: const EdgeInsets.symmetric(horizontal: 45),
                    tabs: tabs
                        .map((e) => Tab(
                      text: e,
                    ))
                        .toList(),
                  ),
                ),
              ];
            },
            body: TabBarView(
              children: [
                PageViewLike(),
                PageViewRecommend(),
                // PageViewFound()
              ],
            ),
          ),
        )


      // Column(
      //   children: [
      //     setAppbar(),
      //     Expanded(
      //       child: PageView(
      //         onPageChanged: (e) {
      //           _tabController.index = e;
      //         },
      //         // physics: const NeverScrollableScrollPhysics(),
      //         controller: _pageController,
      //         children: const [
      //           PageViewLike(),
      //           PageViewRecommend(),
      //           PageViewFound()
      //         ],
      //       ),
      //     )
      //   ],
      // ),
    ))
      ;
  }

  /// appbar
  AppBar setAppbar() {
    return
      AppBar(
        // backgroundColor: Colors.transparent,

        // brightness: Brightness.dark,

      automaticallyImplyLeading: true,
      centerTitle: true,
      title: TabBar(
        // isScrollable: true,
        controller: _tabController,
        indicatorColor: WcaoTheme.primary,
        indicatorSize: TabBarIndicatorSize.tab,
        unselectedLabelColor: WcaoTheme.indexBootBarColor,
        unselectedLabelStyle: const TextStyle(fontWeight: FontWeight.normal),
        labelColor: WcaoTheme.indexBootBarActColor,
        labelStyle: TextStyle(
          fontSize: 16,
          fontWeight: FontWeight.bold,
        ),
        indicatorWeight: 2,
        indicatorPadding: const EdgeInsets.symmetric(horizontal: 36),
        tabs: _tabs,
        onTap: (int page) {
          setState(() {
            curPage = page;
            _pageController.jumpToPage(curPage);
          });
        },
      ),
      actions: [
        // IconButton(
        //   onPressed: () {},
        //   icon: Icon(
        //     Icons.search_sharp,
        //     size: WcaoTheme.fsXl * 1.66,
        //     color: WcaoTheme.indexBootBarColor.withOpacity(.75),
        //   ),
        // )
        SizedBox(
          width: 30, // 指定宽度
          height: 30, // 指定高度
          child: Image(
            image: AssetImage("assets/icon/search.png"),
            fit: BoxFit.fitWidth, // 根据需要选择 BoxFit
          ),
        )


      ],
    );
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
